﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLECDataLayer;
using System.Data;
using System.Collections;

namespace CreateReservations
{
    public partial class _Default : System.Web.UI.Page
    {
        public int PageNumber
        {
            get
            {
                if (ViewState["PageNumber"] != null)
                {
                    return Convert.ToInt32(ViewState["PageNumber"]);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                ViewState["PageNumber"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            categoriesRepeater.DataSource = Categories.GetAllCategories();
            categoriesRepeater.DataBind();

            PagedDataSource pItems = new PagedDataSource();
            pItems.DataSource = TLECDataLayer.Events.GetAllUpcomingEvents().ToList();
            pItems.AllowPaging = true;
            pItems.PageSize = 2;
            pItems.CurrentPageIndex = PageNumber;

            if (pItems.PageCount > 1)
            {
                rptPages.Visible = true;
                ArrayList pages = new ArrayList();
                for (int i = 1; i <= pItems.PageCount; i++)
                {
                    pages.Add((i).ToString());
                }
                rptPages.DataSource = pages;
                rptPages.DataBind();
            }
            else
            {
                rptPages.Visible = false;
            }
            eventsRepeater.DataSource = pItems;
            eventsRepeater.DataBind();
        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
            LoadData();
        }
    }
}
